﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Transactions;
using BS_Gimnasio.General;
using DS_Gimnasio;

using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop;
using System.Reflection;
using System.IO;

namespace BS_Gimnasio.RRHH.Planilla
{
    [DataContract()]
    public class cls_Detalle_Planilla : cls_Base
    {
        #region Variables

        private int _idPlanilla;
        private cls_Empleado _empleado;
        private decimal _porcentajeCargasSociales;
        private decimal _montoCargasSociales;
        private decimal _cantidadHorasExtras;
        private decimal _montoHorasExtras;
        private int _cantidadDiasAusencias;
        private decimal _montoDiasAusencias;
        private int _cantidadDiasDobles;
        private decimal _montoDiasDobles;
        private int _cantidadDiasIncapacidad;
        private decimal _montoDiasIncapacidad;
        private decimal _salarioBruto;
        private decimal _salarioNeto;

        private const string _rutaColilla = @"\Planillas Pagadas\Comprobante_Planilla.docx";

        #endregion

        #region Propiedades

        [DataMember()]
        public int IdPlanilla
        {
            get { return _idPlanilla; }
            set { _idPlanilla = value; }
        }

        [DataMember()]
        public cls_Empleado Empleado
        {
            get { return _empleado; }
            set { _empleado = value; }
        }

        [DataMember()]
        public decimal PorcentajeCargasSociales
        {
            get { return _porcentajeCargasSociales; }
            set { _porcentajeCargasSociales = value; }
        }

        [DataMember()]
        public decimal MontoCargasSociales
        {
            get { return _montoCargasSociales; }
            set { _montoCargasSociales = value; }
        }

        [DataMember()]
        public decimal CantidadHorasExtras
        {
            get { return _cantidadHorasExtras; }
            set { _cantidadHorasExtras = value; }
        }

        [DataMember()]
        public decimal MontoHorasExtras
        {
            get { return _montoHorasExtras; }
            set { _montoHorasExtras = value; }
        }

        [DataMember()]
        public int CantidadDiasAusencias
        {
            get { return _cantidadDiasAusencias; }
            set { _cantidadDiasAusencias = value; }
        }

        [DataMember()]
        public decimal MontoDiasAusencias
        {
            get { return _montoDiasAusencias; }
            set { _montoDiasAusencias = value; }
        }

        [DataMember()]
        public int CantidadDiasDobles
        {
            get { return _cantidadDiasDobles; }
            set { _cantidadDiasDobles = value; }
        }

        [DataMember()]
        public decimal MontoDiasDobles
        {
            get { return _montoDiasDobles; }
            set { _montoDiasDobles = value; }
        }

        [DataMember()]
        public int CantidadDiasIncapacidad
        {
            get { return _cantidadDiasIncapacidad; }
            set { _cantidadDiasIncapacidad = value; }
        }

        [DataMember()]
        public decimal MontoDiasIncapacidad
        {
            get { return _montoDiasIncapacidad; }
            set { _montoDiasIncapacidad = value; }
        }

        [DataMember()]
        public decimal SalarioBruto
        {
            get { return _salarioBruto; }
            set { _salarioBruto = value; }
        }

        [DataMember()]
        public decimal SalarioNeto
        {
            get { return _salarioNeto; }
            set { _salarioNeto = value; }
        }

        [DataMember()]
        public string RutaColilla
        {
            get { return _rutaColilla; }
        } 

        #endregion

        #region Metodos Heredados

        public override void InsertarActualizarRegistro(bool tipoOperacion, ref string mensajeError)
        {
            if (tipoOperacion)
            {
                InsertarRegistro(ref mensajeError);
            }

            else
            {
                ActualizarRegistro(ref mensajeError);
            }
        }

        private void InsertarRegistro(ref string mensajeError)
        {
            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    using (dtacls_GimnasioDataContext db = new dtacls_GimnasioDataContext())
                    {
                        db.SP_Insertar_Detalle_Planilla(IdUsuarioInicioSeccion, Empleado.IdEmpleado, IdPlanilla, PorcentajeCargasSociales, MontoCargasSociales,
                            CantidadHorasExtras, MontoHorasExtras, CantidadDiasAusencias, MontoDiasAusencias, CantidadDiasDobles, MontoDiasDobles,
                            CantidadDiasIncapacidad, MontoDiasIncapacidad, SalarioBruto, SalarioNeto, ref mensajeError);

                        transaction.Complete();
                    }
                }

                catch (SqlException ex)
                {
                    NumeroError = ex.ErrorCode;
                    MensajeError = ex.Message;
                }

                catch (Exception e)
                {
                    MensajeError = e.Message;
                }
            }
        }

        private void ActualizarRegistro(ref string mensajeError)
        {
            throw new NotImplementedException();
        }

        public override cls_Base TraerObjeto()
        {
            throw new NotImplementedException();
        }

        public override bool EliminarRegistro(ref string mensajeError)
        {
            throw new NotImplementedException();
        }

        public override DataTable Listar(int opcion)
        {
            throw new NotImplementedException();
        }

        public override void OtrosProcesos(int opcion, ref object resultado)
        {
            switch (opcion)
            {
                case 1:
                    CrearColillaPago();
                    break;

                default:
                    break;
            }
        }
        
        #endregion

        #region Metodos Propios

        private void CrearColillaPago()
        {
            try
            {
                Word.Application MSWord = new Word.Application();
                Word.Document Documento = new Word.Document();

                object missing = Missing.Value;

                string cDestino_Word = string.Format("{0}.docx", RutaColilla, Empleado.Apellido);
                string cDestino_PDF = string.Format("{0}.pdf", RutaColilla, Empleado.Apellido);

                File.Copy(RutaColilla, (string)cDestino_Word, true);
                Documento = MSWord.Documents.Open(cDestino_Word);

                string bkmark = "FechaPlanilla";
                object paramtr = bkmark;
                Word.Bookmark bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                object rng = bookmark.Range;
                string bookmarkName = bookmark.Name;
                bookmark.Range.Text = DateTime.Now.ToShortDateString();
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "NumeroPlanilla";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = IdPlanilla.ToString();
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "Moneda";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = "Colones";
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "CedulaEmpleado";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = Empleado.Cedula;
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "NombreCompleto";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0} {1}", Empleado.Nombre, Empleado.Apellido);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "salarioBase";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", Convert.ToDecimal(Filtros[0]));
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "salarioDia";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", Convert.ToDecimal(Filtros[1]));
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "PorcCargasSociales";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = PorcentajeCargasSociales.ToString();
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "MontoCargasSociales";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", MontoCargasSociales);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "CantHorasExtras";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", CantidadHorasExtras);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "MontoHorasExtras";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", MontoHorasExtras);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "CantDiasAusencias";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", CantidadDiasAusencias);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "MontoDiasAusencias";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", MontoDiasAusencias);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "CantDiasDobles";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", CantidadDiasDobles);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "MontoDiasDobles";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", MontoDiasDobles);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "CantDiasIncapacidad";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", CantidadDiasIncapacidad);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "MontoDiasIncapacidad";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", MontoDiasIncapacidad);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "SalarioBruto";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", SalarioBruto);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                bkmark = "SalarioNeto";
                paramtr = bkmark;
                bookmark = Documento.Bookmarks.get_Item(ref paramtr);
                rng = bookmark.Range;
                bookmarkName = bookmark.Name;
                bookmark.Range.Text = string.Format("{0:N}", SalarioNeto);
                Documento.Bookmarks.Add(bookmarkName, ref rng);

                Documento.Save();

                Documento.ExportAsFixedFormat(cDestino_PDF, Word.WdExportFormat.wdExportFormatPDF);

                Documento.Save();

                var doc_close = (Microsoft.Office.Interop.Word._Document)Documento;
                doc_close.Close();
            }

            catch (IOException ex)
            {
                MensajeError = ex.Message;

                GC.Collect();
            }

            catch (Exception e)
            {
                MensajeError = e.Message;

                GC.Collect();
            }
        }

        #endregion

    }
}
